﻿using Hotel_Project.Bean;
using Hotel_Project.Tools.MySqlData_Helps;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hotel_Project.BIZ
{
    internal class AddUserTreeView
    {
        public AddUserTreeView() { }    
        public async void add(TreeView tree)
        {
            MySqlDataTable da = new MySqlDataTable();
            DataTable data= await da.GetDataTable(MySqlString.MYSQL_CONNAND,MySqlString.MYSQL_SELECT_st);
            List<MenuStripDomain> list=MySqlGetList.GetDataList<MenuStripDomain>(data);
            foreach (MenuStripDomain domain in list.Where(t => (int)t.GetType().GetProperty("FId").GetValue(t)==-1))
            {
                //添加根节点
                TreeNode tn = new TreeNode(domain.Text);
                tn.Checked = true;
                tree.Nodes.Add(tn);
                zdd(tn,domain.Id,list);
            }
        }
        void zdd(TreeNode Fu, int Rank, List<MenuStripDomain> list)
        {
            foreach (MenuStripDomain domain in list.Where(t => (int)t.GetType().GetProperty("FId").GetValue(t) != -1))
            {
                //传入的Rank就是头节点的标号(每一个节点都有一个唯一性的编号),与子节点的属于那个父节点比较
                if (Rank == Convert.ToInt32(domain.FId.ToString()))
                {
                    //创建一个节点
                    TreeNode tn=new TreeNode(domain.Text);
                    tn.Checked = true;
                    //加入到父节点中
                    Fu.Nodes.Add(tn);
                    //递归调用找字节点下的子节点
                    zdd(tn, domain.Id, list);
                }
            }
        }
    }
}
